#region Directives

using System.Reflection;
using System;
using System.Collections;
using System.Globalization;
using Astro.Core;

#endregion

namespace Astro.Plus
{    
    public class PostSorter
    {
        /// <summary>
        /// sortDirection
        /// </summary>
        private SortDirection sortDirection;

        /// <summary>
        /// propertyInfo
        /// </summary>
        private PropertyInfo propertyInfo;

        /// <summary>
        /// AlbumSorter
        /// </summary>
        /// <param name="propertyName">propertyName</param>
        /// <param name="sortDirection">sortDirection</param>
        public PostSorter(string propertyName, SortDirection sortDirection)
        {
            this.sortDirection = sortDirection;
            this.propertyInfo = typeof(Post).GetProperty(propertyName);

            if (this.propertyInfo == null)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "{0} does not exist", propertyName));
            }
        }

        /// <summary>
        /// SortDirection
        /// </summary>
        public enum SortDirection
        {
            /// <summary>
            /// Ascending
            /// </summary>
            Ascending,

            /// <summary>
            /// Descending
            /// </summary>
            Descending
        }

        /// <summary>
        /// Compare
        /// </summary>
        /// <param name="post1">album1</param>
        /// <param name="post2">album2</param>
        /// <returns>int</returns>
        public int Compare(Post post1, Post post2)
        {
            if (post1 == null)
            {
                throw new ArgumentNullException("post1");
            }

            if (post2 == null)
            {
                throw new ArgumentNullException("post2");
            }

            object propertyValue1 = this.propertyInfo.GetValue(post1, new object[] { });
            object propertyValue2 = this.propertyInfo.GetValue(post2, new object[] { });

            if (this.sortDirection == SortDirection.Ascending)
            {
                IComparable comparer = propertyValue1 as IComparable;

                if (comparer != null)
                {
                    return comparer.CompareTo(propertyValue2);
                }
                else
                {
                    throw new InvalidOperationException("Sort property is not IComparable");
                }
            }
            else
            {
                IComparable comparer = propertyValue2 as IComparable;

                if (comparer != null)
                {
                    return comparer.CompareTo(propertyValue1);
                }
                else
                {
                    throw new InvalidOperationException("Sort property is not IComparable");
                }
            }
        }
    }
}